package com.msw.axon.service;

import com.google.common.collect.Sets;
import com.msw.axon.model.Employee;
import com.msw.axon.model.User;
import com.msw.axon.repository.EmployeeReposity;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

/**
 * Created by minshiwu on 2017/2/18.
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeReposity employeeReposity;

    @Autowired
    private UserService userService;

    @Transactional
    public Employee saveEmpoyee(String name) {
        System.err.println(userService.getClass().getTypeName());
        List<User> list = userService.queryByName(name);
        System.err.println(list);

        Employee employee = new Employee();
        employee.setName(name);
        employee.setUpdateTime(new Timestamp(DateTime.now().getMillis()));
        employee.setNickNames(Sets.newHashSet("nick01", "nick02"));
        return employeeReposity.save(employee);
    }

    public void deleteEmploy(String name) {
        employeeReposity.deleteByName(name);
    }

    public void addNickNames(String name, String nickName) {
        Employee employee = employeeReposity.findByName(name);
        Set<String> set = employee.getNickNames();
        if (set == null) {
            set = Sets.newHashSet();
        }
        set.add(nickName);
        employee.setNickNames(set);
    }

    public void updateEmploy(String name) {

    }

    public List<Employee> findEmployee() {
        return employeeReposity.findAll();
    }
}
